﻿/*
 * BFind - A tool for finding football matches for trading on Betfair
 * Copyright 2009, 2010 Magnus Olsson <magnus72(at)gmail.com>
 * 
 * This file is part of BFind.
 * 
 * BFind is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * BFind is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with BFind.  If not, see <http://www.gnu.org/licenses/>
*/

using System;
using System.Collections.Generic;

namespace betfairfind.Rules
{
    class BTS : BaseRule
    {
        double YES_ODDS_MIN = 1.5;
        double YES_ODDS_MAX = 2;
        double MIN_MATCHED = 500;

        public BTS(Dictionary<string, string> config, List<MyMarket> inplaymarkets, Dictionary<long, MarketPricesCompressed> marketprices)
            : base()
        {
            MarketsUsedInside = new string[] { "both teams to score?" };
            try
            {
                YES_ODDS_MIN = Convert.ToDouble(config["Rule.BTS.YES_LAYODDS_MIN"]);
                YES_ODDS_MAX = Convert.ToDouble(config["Rule.BTS.YES_LAYODDS_MAX"]);
                MIN_MATCHED = Convert.ToDouble(config["Rule.BTS.MIN_MATCHED"]);
            }
            catch (Exception e)
            {
                Logger.LogError(e.ToString(), true, true);

            }

            string criteria = @"" + YES_ODDS_MIN + "/" +
                            YES_ODDS_MAX + "/" +
                            MIN_MATCHED;

            Logger.Log("============================================================================", true, true);
            Logger.Log("Matches qualified for BTS (" + criteria + ")", true, true);
            Logger.Log("----------------------------------------------------------------------------", true, true);

            if (!CheckRequiredMarkets()) { return; }

            string[] games = FindSoccerGames.GetMatches();

            int selectionCount = 0;
            foreach (var game in games)
            {
                try
                {
                    MyMarket market_bts = FindSoccerGames.GetMarket(game, "Both teams to score?");
                    MarketPricesCompressed mpc_bts = marketprices[market_bts.marketId];

                    double yeslayodds = mpc_bts.GetBestLayOdds(0);
                    double matched = mpc_bts.TotalMatched;

                    if ((yeslayodds >= YES_ODDS_MIN && yeslayodds <= YES_ODDS_MAX) && matched >= MIN_MATCHED)
                    {
                        Logger.Log(market_bts.marketTime + ", " + BF.convertPathToGame(game) + ", yes: " + yeslayodds + ", matched " + config["CURRENCY_SIGN"] + matched, true, true);
                        selectionCount++;
                    }
                }
                catch (Exception ex)
                {
                    Logger.LogError("Error in game " + game + Environment.NewLine + ex.ToString(), true, false);
                }

            }
            if (selectionCount == 0)
            {
                Logger.Log("No matches", true, true);
            }
        }

    }
}
